package oop.ex1.sortedStructure;
import java.util.Random;
import java.util.Date;

/**
 * this class is a performance test class for the sorted structures.
 * @author liorz06
 * @author ohad.dan
 */
public class SortedStructureTester {
    /**
     * this method runs the timed test.
     * @param which
     * @param limit
     * @return time that took to run the test
     */
    public static long runTest(int which, int limit){
    	SortedStructure<Integer, Data> struct;
    	if ( which == 0 ) {
    		struct = new SortedList<Integer, Data>();
    	}else {
    		struct = new BinarySearchTree<Integer, Data>();
    	}
    	Random generator = new Random();
    	Date before = new Date();
    	for ( int i=0; i<limit; i++ ){
    		int rand = generator.nextInt(limit*10);
    		Data val = new Data(rand);
    		try{
    		    struct.insert(val);
    		}
    		catch( SortedStructureException e ) {
    		    i--;
    		}
    	}
    	Date after = new Date();
    	return ( after.getTime() - before.getTime() );
    }
    /**
     * Running program - generates statistics by user parameters
     * that enables comparing the running time of the different
     * implementations. 
     * @param args  [0] - Start value for insertions. 
     *          	[1] - Increment value.
     *          	[2] - Number of insertion per structure.
     */
      public static void main(String[] args){
          int startVal, inc, colNum;
          startVal = Integer.parseInt(args[0]);
          inc = Integer.parseInt(args[1]);
          colNum = Integer.parseInt(args[2]);
          for( int i=startVal; i<=colNum; i+=inc ) {
              System.out.printf("%5d|",i );
          }
          System.out.println();
          for( int i=startVal; i<=colNum; i+=inc ) {
              System.out.printf("%5s|","-----");
          }
          System.out.println();
          for( int i=startVal; i<=colNum; i+=inc ) {
              System.out.printf("%5d|",runTest(0,i*1000));
          }
          System.out.println();
          for( int i=startVal; i<=colNum; i+=inc ) {
              System.out.printf("%5d|",runTest(1,i*1000));
          }
      }
}
